package com.jxc.jxc01.grc.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jxc.jxc01.grc.entity.RespResult;
import com.jxc.jxc01.grc.entity.Return;
import com.jxc.jxc01.grc.service.IReturnService;
import com.jxc.jxc01.grc.service.impl.IReturnServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(value = "/queryReturn")
public class QueryReturnServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request,
                           HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        System.out.println("queryReturn");
        RespResult result1 = (RespResult) request.getSession().getAttribute("result");


        IReturnService returnService = new IReturnServiceImpl();
        List<Return> returns = returnService.queryReturn();
        System.out.println(returns);
        if (result1 == null){
            if (returns != null && returns.size() > 0) {
                int count = returnService.getCount();
                RespResult result = new RespResult();
                result.setCode(0);
                result.setMessage("ok");
                result.setCount(count);
                result.setData(returns);
                System.out.println(result);
                writer.write(JSON.toJSONString(result));
            }
        }else {
            writer.println(JSON.toJSONString(result1));
            request.getSession().invalidate();
        }
        writer.close();
    }
}
